package com.tsugun.mediaplayer.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.MusicModel;
import com.tsugun.mediaplayer.view.loader.MediaImageLoad;

import java.util.List;

import static com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper.*;

/**
 * 列表适配器
 * Created by shize on 2017/3/30.
 */

public class MusicFeatureItemAdapter extends RecyclerView.Adapter<MusicFeatureItemAdapter.RecyclerViewHolder> {

    private LayoutInflater mInflater;
    private List<MusicModel> mData;
    private OnMusicItemClickListener mOnMusicItemClickListener;

    public interface OnMusicItemClickListener {
        void onItemClick(int position);

        void onMenuClick(int position);
    }

    public MusicFeatureItemAdapter(Context mContext, List<MusicModel> mData) {
        this.mData = mData;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setOnMusicItemClickListener(OnMusicItemClickListener onMusicItemClickListener) {
        mOnMusicItemClickListener = onMusicItemClickListener;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecyclerViewHolder(mInflater.inflate(R.layout.item_music_feature, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
        holder.mIvIcon.setImageResource(R.drawable.ic_music_default);
        MediaImageLoad.localImageLoad(mData.get(position), holder.mIvIcon);
        holder.mTvTitle.setText(mData.get(position).getTitle());
        holder.mTvArtist.setText(mData.get(position).getArtist());

        if (mOnMusicItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnMusicItemClickListener.onItemClick(holder.getAdapterPosition());
                }
            });
            holder.mIvMenu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnMusicItemClickListener.onMenuClick(holder.getAdapterPosition());
                }
            });
        }

        setSettingTheme(holder);
    }

    // 设置主题色
    private void setSettingTheme(RecyclerViewHolder holder) {
        holder.mTvTitle.setTextColor(INSTANCE.getTEXT_COLOR());
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {

        RoundedImageView mIvIcon;
        TextView mTvTitle;
        TextView mTvArtist;
        ImageView mIvMenu;

        RecyclerViewHolder(View itemView) {
            super(itemView);
            mIvIcon = (RoundedImageView) itemView.findViewById(R.id.id_item_music_icon);
            mTvTitle = (TextView) itemView.findViewById(R.id.id_item_feature_title);
            mTvArtist = (TextView) itemView.findViewById(R.id.id_item_feature_artist);
            mIvMenu = (ImageView) itemView.findViewById(R.id.id_item_feature_menu);
        }
    }
}
